<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="orderNo">
              <el-input v-model="filters.orderNo" clearable placeholder="订单编号"></el-input>
            </el-form-item>
            <el-form-item prop="splitType">
              <el-select v-model="filters.splitType" placeholder="分账订单来源类型">
                <el-option v-for="item in splitTypeList" :value="item.value" :label="item.label" :key="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="splitState">
              <el-select v-model="filters.splitState" clearable filterable placeholder="分账状态">
                <el-option v-for="item in splitStateList" :value="item.value" :label="item.label" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <div class="amount-wrapper">
                <el-input type="number" v-model="filters.minSplitAmount" placeholder="最少金额"> </el-input>
                <div class="separate"></div>
                <el-input type="number" v-model="filters.maxSplitAmount" placeholder="最多金额"> </el-input>
              </div>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table ref="tableList" :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column align="center" label="批次码" prop="batchCode"> </el-table-column>
        <el-table-column align="center" label="分账订单来源类型">
          <template slot-scope="scope">
            <div>
              {{ scope.row.splitType == 1 ? '自订单（包含充值订单）' : '公司补贴订单' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="支付金额(扣除手续费之前)" prop="amount"> </el-table-column>
        <el-table-column align="center" label="可分账金额（扣除手续费后）" prop="balance"> </el-table-column>
        <el-table-column align="center" label="已分账金额" prop="outBalance"> </el-table-column>
        <el-table-column align="center" label="分账状态">
          <template slot-scope="scope">
            <el-tag effect="dark" size="mini" :type="formatSplitStates(scope.row.splitState)">
              {{ formatSplitState(scope.row.splitState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import * as message from '@/constants/message';
import OrderSplitAccountApi from '@/api/account/orderSplitAccount';
export default {
  name: 'OrderSplitAccountManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      listLoading: true,
      filters: {
        orderNo: '',
        minSplitAmount: '',
        maxSplitAmount: '',
        splitState: '',
        splitType: '',
      },
      dataList: [],
      splitTypeList: [
        { value: 1, label: '自订单（包含充值订单）' },
        { value: 2, label: '公司补贴订单' },
      ],
      splitStateList: [
        { value: 0, label: '未分账' },
        { value: 1, label: '分账中' },
        { value: 2, label: '已分账' },
      ],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionCheck: 'SYSTEM_MANAGER_ACCOUNT_ORDER_SPLIT_ACCOUNT_LIST_SEE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {},
  watch: {
    //to去哪，from从哪来
    // $route(to,from){
    //   if(from.path =='/equip/specimenEdit' || from.path=='/equip/specimenAdd'){
    //       this.listData();
    //   }
    // }
  },
  methods: {
    init() {
      this.listLoading = true;
      this.listData();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },
    handleClickDetail(row) {
      this.$router.push({
        path: '/account/orderSplitAccountDetail',
        query: { id: row.splitAccountId },
      });
    },
    formatSplitStates(val) {
      switch (val) {
        case 0:
          return 'danger';
        case 1:
          return 'primary';
        case 2:
          return 'success';
      }
    },
    formatSplitState(val) {
      switch (val) {
        case 0:
          return '未分账';
        case 1:
          return '待分账';
        case 2:
          return '已分账';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.filters.maxSplitAmount = '';
      this.filters.minSplitAmount = '';
      this.onSearch();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 获取列表数据
      const { orderNo, minSplitAmount, maxSplitAmount, splitState, splitType } = this.filters;
      if (minSplitAmount > 0 && maxSplitAmount > 0 && parseFloat(minSplitAmount) > parseFloat(maxSplitAmount)) {
        this.$message.error('最大金额必须大于最小金额');
        return;
      }
      let cond = {
        pageNo: this.page.pageNo,
        maxSplitAmount,
        minSplitAmount,
        pageSize: this.page.pageSize,
        orderNo,
        splitState,
        splitType,
      };
      this.listLoading = true;
      OrderSplitAccountApi.listOrderSplitAccount(cond)
        .then((res) => {
          this.dataList = res.data.list;
          this.page.pageTotal = res.data.total;
        })
        .finally(() => {
          this.listLoading = false;
          this.$refs.tableList.doLayout();
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
